package com.knife.blog.util;

import java.io.*;
import java.util.zip.*;

import com.knife.blog.domain.BlogNewsDoc;
import com.knife.blog.domain.Photo;
import com.knife.util.FileUtil;
import com.knife.web.Globals;

public class BlogFileUtil extends FileUtil {
	public final static String AUDIO_FILE_EXT = "mp3;wma";

	public boolean unZip(String zipFileName, String outputDirectory) {
		boolean ret = false;
		ZipInputStream in;
		ZipEntry z;
		try {
			in = new ZipInputStream(new FileInputStream(zipFileName));
			while ((z = in.getNextEntry()) != null) {
				System.out.println("unziping " + z.getName());
				if (z.isDirectory()) {
					String name = z.getName();
					name = name.substring(0, name.length() - 1);
					File f = new File(outputDirectory + File.separator + name);
					f.mkdir();
					System.out.println("新建一个目录："+f.getName());
					System.out.println("mkdir " + outputDirectory + File.separator
							+ name);
				} else {
					File f = new File(outputDirectory + File.separator
							+ z.getName());
					f.createNewFile();
					FileOutputStream out = new FileOutputStream(f);
					int b;
					while ((b = in.read()) != -1)
						out.write(b);
					ret = true;
					out.close();
				}
			}
			in.close();
		} catch (FileNotFoundException e) {
			ret = false;
			e.printStackTrace();
		} catch (IOException e) {
			ret = false;
			e.printStackTrace();
		}
		return ret;
	}
	
	public static boolean removeFile(String filePath){
		File file = new File(filePath);
		return file.exists() && file.delete();
	}
	public static boolean removeDire(String Direpath){
		boolean retv=false;
		File file=new File(Direpath);
		if(file.isDirectory()){
			retv=delFolder(Direpath);
		}
		return retv;
	}
//	删除文件夹
//	param folderPath 文件夹完整绝对路径

	  public static boolean delFolder(String folderPath) {
		  boolean retv=false;
	     try {
	    	 retv=delAllFile(folderPath); //删除完里面所有内容
	        String filePath = folderPath;
	        filePath = filePath.toString();
	        java.io.File myFilePath = new java.io.File(filePath);
	        myFilePath.delete(); //删除空文件夹
	     } catch (Exception e) {
	       e.printStackTrace(); 
	     }
	     return retv;
	}

//	删除指定文件夹下所有文件
//	param path 文件夹完整绝对路径
	   public static boolean delAllFile(String path) {
	       boolean flag = false;
	       File file = new File(path);
	       if (!file.exists()) {
	         return flag;
	       }
	       if (!file.isDirectory()) {
	         return flag;
	       }
	       String[] tempList = file.list();
	       File temp = null;
	       for (int i = 0; i < tempList.length; i++) {
	          if (path.endsWith(File.separator)) {
	             temp = new File(path + tempList[i]);
	          } else {
	              temp = new File(path + File.separator + tempList[i]);
	          }
	          if (temp.isFile()) {
	             temp.delete();
	             flag = true;
	          }
	          if (temp.isDirectory()) {
	             delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件
	             delFolder(path + "/" + tempList[i]);//再删除空文件夹
	             flag = true;
	          }
	       }
	       return flag;
	     }
	public boolean isAudioFile(String fileName){
		boolean ret;
		ret = checkExtFile(AUDIO_FILE_EXT,fileName);
		return ret;
		
	}
	public static void main(String[] args){
		BlogFileUtil fil=new BlogFileUtil();
		fil.unZip("C:\\Tomcat5.5.27\\webapps\\ROOT\\WEB-INF\\templates\\blog\\template\\Elan.rar","C:\\Tomcat5.5.27\\webapps\\ROOT\\WEB-INF\\templates\\blog\\template");
	}
}
